/**
 * Created by quxiangqian on 2018/6/9.
 */
export class DataSource{
    constructor(){
        //window.datasouce=this;
        this.method="post"
        this.success=[];
    }

    /**
     * 新增一个成功处理方法，当一个数据源头产生多种不同处理方式的时候使用
     * @param fun
     */
    addSuccess=(fun)=>{
        this.success.push(fun)
    }

    clear=()=>{
        this.success=[];
    }

    bindForm=(form)=>{
        this.data=form.getData();
    }
    /**
     * 加载数据源
     */
    load=()=>{
        $.ajax({
            url:this.url,
            type:this.method,
            data:this.data,
            dataType:"json",
            beforeSend: (XMLHttpRequest)=> {
               // sessionStorage.
                let xheader= sessionStorage.getItem("X-header");
                console.log(xheader);
                if(xheader){
                    let header=JSON.parse(xheader);
                    for(let key in header){
                        XMLHttpRequest.setRequestHeader(key, header[key]);
                    }
                    XMLHttpRequest.setRequestHeader("X-Requested-With",null);
                    XMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }

            },
            crossDomain:true,
            success:(data)=>{
                this.success.map((fun)=>{fun(data)})
            },
            error:(jqXHR, textStatus, errorThrown)=>{
                if(window.global["error"]){
                    window.global["error"].show("系统错误，请与管理员联系")
                }

            }
        })

    }

}